﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Text.RegularExpressions;

/** 
 ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
 * Description: 
 * Author: Heller (HellerSong@outlook.com)
 * CreationTime: 3/28/2015 6:31:07 PM
 ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** 
 **/
namespace DevGrammar.TestBasic
{
	[TestClass]
	public class TestRegex
	{
		[TestMethod]
		public void test_regex_match()
		{
			// Match the series number at least one-digit
			Regex myRegex = new Regex(@"\d+");
			Match matchResult = myRegex.Match("Here is a test string.");
			Console.WriteLine(matchResult.Success);

			matchResult = myRegex.Match("Here is a test 2234 string333.");
			Console.WriteLine(matchResult.Success);
			Console.WriteLine("--" + matchResult.Value + "--"); // Not any space around
			Console.WriteLine(matchResult.Length);

			// Get all matched collection
			foreach (Match m in myRegex.Matches("Here is a test 2234 string333."))
				Console.WriteLine(m.Value);
		}

		[TestMethod]
		public void test_match_each_word_in_sentence()
		{
			// Add beginning(^) and end($), will match not any word.
			Regex myRegex = new Regex("^[A-Za-z][a-z]+$");
			String str1 = "Here is a test String";
			Console.WriteLine("Match: " + str1);
			foreach (Match m in myRegex.Matches(str1))
				Console.WriteLine(m.Value);

			Regex myRegex2 = new Regex("[A-Za-z][a-z]+");
			String str2 = "firstName";
			Console.WriteLine("Match: " + str2);
			foreach (Match m in myRegex2.Matches(str2))
				Console.WriteLine(m.Value);

			String str3 = "add_user";
			Console.WriteLine("Match: " + str3);
			foreach (Match m in myRegex2.Matches(str3))
				Console.WriteLine(m.Value);

			// Will match nothing
			String str4 = "ADD_USER";
			Console.WriteLine("Match: " + str4);
			foreach (Match m in myRegex2.Matches(str4))
				Console.WriteLine(m.Value);
		}

		[TestMethod]
		public void test_match_ADD_USER()
		{
			Regex myRegex = new Regex("[A-Za-z]([a-z]+|[A-Z]+)");
			String str = "ADD_USER";
			Console.WriteLine("Match: " + str);
			foreach (Match m in myRegex.Matches(str))
				Console.WriteLine(m.Value);
		}

		[TestMethod]
		public void test_match_specified_word()
		{
			Regex myRegex = new Regex(@"^(D|d)oes\w*");
			String str = "doesContianMethod";
			Console.WriteLine("Match: " + str);
			Console.WriteLine(myRegex.IsMatch(str));

			String str2 = "DoesContain";
			Console.WriteLine("Match: " + str2);
			Console.WriteLine(myRegex.IsMatch(str2));

			String str3 = "contain";
			Console.WriteLine("Match: " + str3);
			Console.WriteLine(myRegex.IsMatch(str3));
		}

	}
}
